﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace DienLib.SummitLib
{
    /// <summary>
    /// Thread-safe Wrapper for ComboBox in Summit Application
    /// All Member are Thread-Safe
    /// </summary>
    class ComboBoxD
    {
        private ComboBox _refobj;
        public ComboBoxD(ComboBox refComboBox)
        {
            _refobj = refComboBox;
        }

        public object DataSource
        {
            get
            {
                if (_refobj.InvokeRequired) return (object)_refobj.Invoke((Func<object>)delegate() { return _refobj.DataSource; });
                else return _refobj.DataSource;
            }
            set
            {
                DienLib.WinAPI.DrawRevFrame(_refobj.Handle); System.Threading.Thread.Sleep(1000);
                if (_refobj.InvokeRequired) _refobj.Invoke((MethodInvoker)delegate() { _refobj.DataSource = value; });
                else _refobj.DataSource = value;
                DienLib.WinAPI.DrawRevFrame(_refobj.Handle); System.Threading.Thread.Sleep(1000);
            }
        }

        public string Text
        {
            get
            {
                if (_refobj.InvokeRequired) return (string)_refobj.Invoke((Func<string>)delegate() { return _refobj.Text; });
                else return _refobj.Text;
            }
            set
            {
                DienLib.SummitLib.Command.BlinkBorderBefore(_refobj);
                if (_refobj.InvokeRequired) _refobj.Invoke((MethodInvoker)delegate() { _refobj.Text = value; });
                else _refobj.Text = value;
                DienLib.SummitLib.Command.BlinkBorderAfter(_refobj);
            }
        }

        
        #region DISPOSE METHOD
        // Dispose() calls Dispose(true)
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        // NOTE: Leave out the finalizer altogether if this class doesn't 
        // own unmanaged resources itself, but leave the other methods
        // exactly as they are. 
        ~ComboBoxD() 
        {
            // Finalizer calls Dispose(false)
            Dispose(false);
        }

        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected virtual void Dispose(bool disposing)
        {
            if (disposing) 
            {
                // free managed resources, (detach event ?)
                if (this._refobj != null) this._refobj = null; // Khong keu this._refobj.Dispose vi Reference toi object do. Ngoai ra do pass reference nen this._refobj = null khong anh huong gi toi object goc
            }

            /* free native resources if there are any.
            if (nativeResource != IntPtr.Zero) 
            {
                Marshal.FreeHGlobal(nativeResource);
                nativeResource = IntPtr.Zero;
            }*/
        }
        #endregion
    
    }
}
